package com.uidt.project.ptsj.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.uidt.common.constant.Constants;
import com.uidt.common.utils.StringUtils;
import com.uidt.common.utils.file.FileUtils;
import com.uidt.common.utils.uuid.UUID;
import com.uidt.framework.config.RuoYiConfig;
import com.uidt.project.ptsj.domain.PtsjFile;
import com.uidt.project.ptsj.domain.PtsjScreenDzzd;
import com.uidt.project.ptsj.service.IPtsjFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Service;
import com.uidt.project.ptsj.mapper.PtsjScreenArticleMapper;
import com.uidt.project.ptsj.domain.PtsjScreenArticle;
import com.uidt.project.ptsj.service.IPtsjScreenArticleService;

/**
 * 大屏列内容，包括闭环管理、部门共治、行业自治、平台协同Service业务层处理
 * 
 * @author uidt
 * @date 2024-09-23
 */
@Service
public class PtsjScreenArticleServiceImpl implements IPtsjScreenArticleService 
{
    @Autowired
    private PtsjScreenArticleMapper ptsjScreenArticleMapper;

    @Autowired
    private IPtsjFileService ptsjFileService;

    /**
     * 查询大屏列内容，包括闭环管理、部门共治、行业自治、平台协同
     * 
     * @param id 大屏列内容，包括闭环管理、部门共治、行业自治、平台协同主键
     * @return 大屏列内容，包括闭环管理、部门共治、行业自治、平台协同
     */
    @Override
    public PtsjScreenArticle selectPtsjScreenArticleById(String id)
    {
        PtsjScreenArticle article = ptsjScreenArticleMapper.selectPtsjScreenArticleById(id);
        PtsjFile file = new PtsjFile();
        file.setTablename("ptsj_screen_article");
        file.setGlid(article.getId());
        List<PtsjFile> fileList = ptsjFileService.selectPtsjFileList(file);

        if(fileList!=null && fileList.size()>0){
            article.setFileList(fileList);
        }

        return article;
    }

    /**
     * 查询大屏列内容，包括闭环管理、部门共治、行业自治、平台协同列表
     * 
     * @param ptsjScreenArticle 大屏列内容，包括闭环管理、部门共治、行业自治、平台协同
     * @return 大屏列内容，包括闭环管理、部门共治、行业自治、平台协同
     */
    @Override
    public List<PtsjScreenArticle> selectPtsjScreenArticleList(PtsjScreenArticle ptsjScreenArticle)
    {
        List<PtsjScreenArticle> articleList = ptsjScreenArticleMapper.selectPtsjScreenArticleList(ptsjScreenArticle);

        List<String> glids = new ArrayList<>();
        for(PtsjScreenArticle article:articleList)
        {
            glids.add(article.getId());
        }

        if(glids.size()>0){
            List<PtsjFile> fileList = ptsjFileService.selectPtsjFileListByGlid(glids,"ptsj_screen_article");
            if(fileList!=null && fileList.size()>0){
                for(PtsjScreenArticle article:articleList){
                    List<PtsjFile> ptsjFileList = new ArrayList<>();
                    Iterator<PtsjFile> iterable = fileList.iterator();
                    while (iterable.hasNext()){
                        PtsjFile file = iterable.next();
                        if(article.getId().equals(file.getGlid())){
                            ptsjFileList.add(file);
                            iterable.remove();
                        }
                    }
                    article.setFileList(ptsjFileList);
                }
            }
        }

        return articleList;
    }

    /**
     * 新增大屏列内容，包括闭环管理、部门共治、行业自治、平台协同
     * 
     * @param ptsjScreenArticle 大屏列内容，包括闭环管理、部门共治、行业自治、平台协同
     * @return 结果
     */
    @Override
    public int insertPtsjScreenArticle(PtsjScreenArticle ptsjScreenArticle)
    {
        String id = UUID.randomUUID().toString();
        ptsjScreenArticle.setId(id);
        ptsjScreenArticle.setDel("0");

        int count = ptsjScreenArticleMapper.insertPtsjScreenArticle(ptsjScreenArticle);
        String glid =id;
        if(count!=0 && ptsjScreenArticle.getFileList()!=null){
            for(PtsjFile ptsjFile:ptsjScreenArticle.getFileList()){
                ptsjFile.setTablename("ptsj_screen_article");
                ptsjFile.setGlid(glid);
                ptsjFileService.insertPtsjFile(ptsjFile);
            }
        }

        return count;
    }

    /**
     * 修改大屏列内容，包括闭环管理、部门共治、行业自治、平台协同
     * 
     * @param ptsjScreenArticle 大屏列内容，包括闭环管理、部门共治、行业自治、平台协同
     * @return 结果
     */
    @Override
    public int updatePtsjScreenArticle(PtsjScreenArticle ptsjScreenArticle)
    {
        int count = ptsjScreenArticleMapper.updatePtsjScreenArticle(ptsjScreenArticle);

        // 获取原先的附件列表
        PtsjFile file = new PtsjFile();
        file.setGlid(ptsjScreenArticle.getId());
        file.setTablename("ptsj_screen_article");
        List<PtsjFile> oldfileList = ptsjFileService.selectPtsjFileList(file);

        // 获取更新的数据中，原先的附件id列表，这些是不需要删除的附件
        List<Long> stayFileIds = new ArrayList<>();
        if(ptsjScreenArticle.getFileList()!=null && ptsjScreenArticle.getFileList().size()>0){
            for(PtsjFile file1:ptsjScreenArticle.getFileList()){
                if(file1.getId()!=null && StringUtils.isNotEmpty(file1.getGlid())){
                    stayFileIds.add(file1.getId());
                }else{
                    // 没有id和关联id的说明是本次新加的附件，需要入库
                    file1.setGlid(ptsjScreenArticle.getId());
                    file1.setTablename("ptsj_screen_article");
                    ptsjFileService.insertPtsjFile(file1);
                }
            }
        }
        for(PtsjFile olFile:oldfileList){
            // 原先的附件id没有在这次更新的附件列表中，那就需要删除原先的附件文件，以及附件关联记录
            if(!stayFileIds.contains(olFile.getId())){
                // 删除文件
                FileUtils.deleteFile(RuoYiConfig.getProfile()+StringUtils.substringAfter(olFile.getFilename(), Constants.RESOURCE_PREFIX));
                // 删除数据库记录
                ptsjFileService.deletePtsjFileById(olFile.getId());
            }
        }



        return count;
    }

    /**
     * 批量删除大屏列内容，包括闭环管理、部门共治、行业自治、平台协同
     * 
     * @param ids 需要删除的大屏列内容，包括闭环管理、部门共治、行业自治、平台协同主键
     * @return 结果
     */
    @Override
    public int deletePtsjScreenArticleByIds(String[] ids)
    {
        return ptsjScreenArticleMapper.deletePtsjScreenArticleByIds(ids);
    }

    /**
     * 删除大屏列内容，包括闭环管理、部门共治、行业自治、平台协同信息
     * 
     * @param id 大屏列内容，包括闭环管理、部门共治、行业自治、平台协同主键
     * @return 结果
     */
    @Override
    public int deletePtsjScreenArticleById(String id)
    {
        return ptsjScreenArticleMapper.deletePtsjScreenArticleById(id);
    }

    @Override
    public int changeTop(PtsjScreenArticle ptsjScreenArticle) {

        int count = ptsjScreenArticleMapper.updatePtsjScreenArticle(ptsjScreenArticle);

        return count;
    }

    @Override
    public int changeDel(PtsjScreenArticle ptsjScreenArticle) {
        int count = ptsjScreenArticleMapper.updatePtsjScreenArticle(ptsjScreenArticle);
        return count;
    }
}
